//
//  GFNearResturtantTableViewCell.m
//  GoodFood
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFNearResturtantTableViewCell.h"


@interface GFNearResturtantTableViewCell ()
@property (nonatomic, strong) UIImageView *cateImageView;
@property (nonatomic, strong) UILabel *cateLabel;
@property (nonatomic, strong) UIImageView *disImageView;
@property (nonatomic, strong) UILabel *distanceLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *starImageView;
@property (nonatomic, strong) UILabel *everyPriceLabel;
@end

@implementation GFNearResturtantTableViewCell

#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatImageView];
        [self creatLabel];
    }
    return self;
}
#pragma mark - set
- (void)setModel:(GFNearResturtantModel *)model {
    if (_model != model) {
        _model = model;
    }
    self.cateImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"category_%ld.png",model.category_id]];
    self.cateLabel.text = self.model.category;
    self.disImageView.image = [UIImage imageNamed:@"iconfont-juli"];
    
    
    if (model.distance <= 100) {
        self.distanceLabel.text = @"100米以内";
    } else if (model.distance > 100 && model.distance <= 1000) {
        self.distanceLabel.text = [NSString stringWithFormat:@"距离%ld米", model.distance];
    } else {
        self.distanceLabel.text = [NSString stringWithFormat:@"%.1f公里", (float)model.distance / 1000];
    }
    
//    self.distanceLabel.text = [NSString stringWithFormat:@"距离%ld米",self.model.distance];
    self.titleLabel.text = self.model.place_name;
    NSString *star = [NSString stringWithFormat:@"%.1f", self.model.star];
    NSRange rangeForFloat = {2,1};
    NSRange rangeForInt = {0,1};
    for (int i = 0; i < 5; i ++) {
        UIImageView *imageView = (UIImageView *)[self.contentView viewWithTag:100 + i];
        imageView.image = [UIImage imageNamed:@"wuxing.png"];
        NSString *strTail = [star substringWithRange:rangeForFloat];
        NSString *strHead = [star substringWithRange:rangeForInt];
        if ([strTail isEqualToString:@"0"]) {
            for (int j = 0; j < [strHead integerValue]; j ++) {
                UIImageView *imageView2 = (UIImageView *)[self.contentView viewWithTag:100 + j];
                imageView2.image = [UIImage imageNamed:@"quanxing.png"];
            }
        } else {
            for (int j = 0; j < [strHead integerValue]; j ++) {
                UIImageView *imageView2 = (UIImageView *)[self.contentView viewWithTag:100 + j];
                imageView2.image = [UIImage imageNamed:@"quanxing.png"];
            }
            UIImageView *imageView3 = (UIImageView *)[self.contentView viewWithTag:100 + [strHead integerValue]];
            imageView3.image = [UIImage imageNamed:@"banxing.png"];
        }
        
    }
    
    NSString *everyPrice = [NSString stringWithFormat:@"%ld", self.model.price];
    if ([everyPrice isEqualToString:@"0"]) {
        self.everyPriceLabel.text = @"";
    } else {
        self.everyPriceLabel.text = [NSString stringWithFormat:@"人均约%@", everyPrice];
    }
}
#pragma mark - 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    self.cateImageView.frame = CGRectMake(ScreenWidth / 20, self.contentView.frame.size.height / 5, ScreenWidth / 4, ScreenWidth / 4);
    CGRect rectForCate = [self.model.category boundingRectWithSize:CGSizeMake(ScreenWidth / 2, 0) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    self.cateLabel.frame = CGRectMake(ScreenWidth / 20 + ScreenWidth / 4, self.contentView.frame.size.height / 5, rectForCate.size.width, self.contentView.frame.size.height / 4);
    
    self.disImageView.frame = CGRectMake(ScreenWidth / 20 + ScreenWidth / 4 + rectForCate.size.width + 5, self.contentView.frame.size.height / 3.8, self.contentView.frame.size.height / 8, self.contentView.frame.size.height / 8);
    self.distanceLabel.frame = CGRectMake(ScreenWidth / 20 + ScreenWidth / 4 + rectForCate.size.width + 10 + self.contentView.frame.size.height / 8, self.contentView.frame.size.height / 5, ScreenWidth / 2, self.contentView.frame.size.height / 4);
    self.titleLabel.frame = CGRectMake(ScreenWidth / 20 + ScreenWidth / 4 , self.contentView.frame.size.height / 5 + self.contentView.frame.size.height / 4, ScreenWidth - ScreenWidth / 20 - ScreenWidth / 4 , self.contentView.frame.size.height / 4);
    for (int i = 0; i < 5; i ++) {
        UIImageView *imageView = (UIImageView *)[self.contentView viewWithTag:100 + i];
        imageView.frame = CGRectMake(ScreenWidth / 20 + ScreenWidth / 4 + (self.contentView.frame.size.height / 8) * i, self.contentView.frame.size.height / 5 + self.contentView.frame.size.height / 4 * 2, self.contentView.frame.size.height / 8, self.contentView.frame.size.height / 8);

    }
    self.everyPriceLabel.frame = CGRectMake(ScreenWidth / 20 + ScreenWidth / 4 + (self.contentView.frame.size.height / 8) * 5, self.contentView.frame.size.height / 5 + self.contentView.frame.size.height / 4 * 2, ScreenWidth / 3, self.contentView.frame.size.height / 8);
    
}
#pragma mark - 创建imageView
- (void)creatImageView {
    self.cateImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.cateImageView];
    
    self.disImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.disImageView];
    
    for (int i = 0; i < 5; i ++) {
        self.starImageView = [[UIImageView alloc] init];
        self.starImageView.tag =  100 + i;
        [self.contentView addSubview:self.starImageView];
    }
}
#pragma mark - 创建label
- (void)creatLabel {
    self.cateLabel = [[UILabel alloc] init];
    self.cateLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.cateLabel];
    
    self.distanceLabel = [[UILabel alloc] init];
    self.distanceLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.distanceLabel];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.titleLabel];
    
    self.everyPriceLabel = [[UILabel alloc] init];
    self.everyPriceLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:self.everyPriceLabel];
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
